
using ApiDemo.Application.Common.Interface;
using ApiDemo.Domain.Base;
using Microsoft.EntityFrameworkCore;

namespace ApiDemo.Infrastructure.Persistence
{
    public class RepositoryBase<T> : IRepository<T> where T : BaseEntity
    {
        private readonly ApiDemoDbContext _db;
        private readonly DbSet<T> _table;
        private readonly ISessionUserService _sessionUserService;
        public RepositoryBase(ApiDemoDbContext db,ISessionUserService sessionUserService)
        {
            _db = db;
            _table = _db.Set<T>();
            _sessionUserService=sessionUserService;
        }

        public IQueryable<T> Table => _table.AsNoTracking();

        public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
        {
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedBy=_sessionUserService.UserId;
            entity.UpdatedBy=_sessionUserService.UserId;
            entity.CreatedAt = DateTime.UtcNow;
            entity.UpdatedAt = DateTime.UtcNow;
            entity.DisplayOrder = default;
            await _table.AddAsync(entity);
            await _db.SaveChangesAsync();
            return entity;
        }

        public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default, bool canHardDelete = false)
        {
            var entity = await this.GetByIdAsync(id);
            if (entity != null)
            {
                await this.DeleteAsync(entity);
            }
        }

        public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default, bool canHardDelete = false)
        {
            // 如果为true，则实际删除记录，否则为伪删除/软删除;默认为false
            if (canHardDelete)
            {
                _table.Remove(entity);
                await _db.SaveChangesAsync();
            }
            else
            {
                entity.IsDeleted = true;
                entity.UpdatedBy=_sessionUserService.UserId;
                await this.UpdateAsync(entity);
            }

        }

        public async Task DeleteRangeAsync(IEnumerable<T> entities, CancellationToken cancellationToken = default, bool canHardDelete = false)
        {
            // 如果为true，则实际删除记录，否则为伪删除/软删除;默认为false
            if (canHardDelete)
            {
                _table.RemoveRange(entities);
                await _db.SaveChangesAsync();
            }
            else
            {
                foreach (var entity in entities)
                {
                    entity.IsDeleted = true;
                    entity.UpdatedAt = DateTime.UtcNow;
                    entity.UpdatedBy=_sessionUserService.UserId;
                }
                _table.UpdateRange(entities);
                await _db.SaveChangesAsync();
            }

        }

        public async Task<T?> GetByIdAsync(Guid id)
        {
            var entity = await _table.FindAsync(id);
            return entity;
        }

        public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
        {
            entity.UpdatedAt = DateTime.UtcNow;
            entity.UpdatedBy=_sessionUserService.UserId;
            _table.Update(entity);
            await _db.SaveChangesAsync();
        }
    }
}